package cn.home.controller;

import cn.home.entity.books;
import cn.home.mapper.BooksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 邹洪辉
 * @version 1.0
 * @date 2023/3/2 - 23:46
 */
@Controller
public class BooksController {
    @Resource
    private BooksMapper booksMapper;

    @Secured("ROLE_list")
    @RequestMapping("/booksList")//遍歷書籍
    public String booksList(Model model){
        List<books> list =booksMapper.getAllBooks();
        model.addAttribute("book",list);
        return "booksList";
    }
    @Secured("ROLE_edit")
    @RequestMapping("/editLogin")//进入修改
    public String editLogin(Model model,Integer bookId){
        books list = booksMapper.listById(bookId);
        model.addAttribute("book",list);
        return "edit";
    }
    @Secured("ROLE_edit")
    @RequestMapping("/editBook")//修改功能
    public String editBook(books books){
        booksMapper.updateById(books);
        return "redirect:/booksList";
    }
    @Secured("ROLE_add")
    @RequestMapping("/addLogin")//进入新增
    public String addLogin(){
        return "add";
    }
    @Secured("ROLE_add")
    @RequestMapping("/addBook")//新增书籍
    public String addBook(books books){
        System.out.println(books);
        booksMapper.addBooks(books);
        return "redirect:/booksList";
    }
    @Secured("ROLE_delete")
    @RequestMapping("/booksDeleted")//刪除功能
    public String booksDeleted(Integer bookId){
        booksMapper.deleteById(bookId);
        return "redirect:/booksList";
    }
}
